import csv
import os


def browser_an():
    data = []
    csv_list = os.listdir("./csv文件/各城市游记浏览量/")
    cnt = 0
    # with open("./csv文件/马蜂窝游记浏览量前30的城市.csv", "w+", newline="", encoding="utf-8") as csv_f:
    with open("./可视化/马蜂窝游记浏览量排行.csv", "w+", newline="", encoding="utf-8") as csv_f:
        writer = csv.writer(csv_f)
        writer.writerow(["城市", "浏览量"])
        for i in csv_list:
            if i == "汇总.csv":
                continue
            total_browser = 0
            # if cnt > 30:
            #     break
            with open("./csv文件/各城市游记浏览量/%s" % i, 'r', encoding="utf-8") as f:
                cnt += 1
                # print(cnt)
                for line in f:
                    if line[0] == "城":  # 读到文件头就跳过
                        continue

                    std_line = line.replace("\"", "")  # 文件读入的时候每个字符都是一个split
                    std_line = std_line.split(",")
                    print(std_line)
                    browser = int(std_line[2].replace("\n", ""))
                    # print("该条游记浏览量为：%d" % browser)
                    total_browser += browser
                    # print("当前总浏览量为：%d" % total_browser)

                data.append((i.replace(".csv", ""), total_browser))
                # line = ["%s" % i.replace(".csv", ""), "%s" % total_browser]
                # writer.writerow(line)
        data = sorted(data, key=lambda x: x[1], reverse=True)
        print(data)
        # for i in range(30):
        for i in range(len(data)):
            line = data[i]
            line = [line[0], line[1]]
            # print(line)
            writer.writerow(line)


def sold_an():
    with open('./csv文件/去哪儿各省份2023.1旅游数据统计.csv', "w", newline="", encoding="utf-8") as new:
        writer = csv.writer(new)
        writer.writerow(["省份", "销量", "景区平均票价"])
        with open("./csv文件/去哪儿各景区2023.1月销.csv", "r", encoding="utf-8") as csv_f:
            new_line = []
            sold_dict = {}
            price_dict = {}
            num_dict = {}
            lines = csv_f.read().split("\n")
            for line in lines:

                line = line.split(",")
                if line[0] == "景点":
                    continue
                # elif line[4] == '香港':
                else:
                    if not line[4] in sold_dict.keys():
                        print("初始化")
                        sold_dict[line[4]] = int(line[2])
                        price_dict[line[4]] = float(line[3])
                        # print("当前的总价格为：", price_dict)
                        num_dict[line[4]] = 1
                    else:
                        print("新增")
                        sold_dict[line[4]] = int(sold_dict[line[4]]) + int(line[2])
                        # print("该景区的价格为：", float(line[3]))
                        price_dict[line[4]] = price_dict[line[4]] + float(line[3])
                        # print("当前的总价格为：", price_dict)
                        num_dict[line[4]] += 1
                # else:
                #     continue
            print(sold_dict)
            print(price_dict)
            print(num_dict)
            province_list = list(sold_dict.keys())
            sold_list = list(sold_dict.values())
            price_list = list(price_dict.values())
            num_list = list(num_dict.values())

            for i in range(len(province_list)):
                average_price = round((price_list[i] / num_list[i]), 2)
                new_line.append([province_list[i], sold_list[i], average_price])

            print(new_line)
            for i in new_line:
                writer.writerow(i)


if __name__ == '__main__':
    # browser_an()
    sold_an()
